package com.archie.zhufengfm.client;

import android.util.Log;

import com.archie.zhufengfm.utils.HttpTools;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by Archie on 2015/10/20.
 */
public final class ClientAPI {

    // 单独提取服务器地址的部分，避免以后更换服务器地址
    private static final String API_POINT = "http://mobile.ximalaya.com";

    private ClientAPI(){



    }

    //------------------------------------------------------------

    // 接口 17 专辑详情

    /**
     * /mobile/others/ca/album/track/203355/true/1/20?device=android&pageSize=20&albumId=203355&isAsc=true
     * @return
     */
    public static JSONObject getAlbums(long albumId, long trackId, int pager){
        JSONObject ret = null;

        String str =
                String.format("/mobile/others/ca/album/track/%d/true/%d/20?device=android&pageSize=20&albumId=203355&isAsc=true", albumId, pager);

        byte[] data = HttpTools.doGet(API_POINT + str);

        if (data != null) {

            try {

                String string = new String(data, "UTF-8");

                ret = new JSONObject(string);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        return ret;
    }


    // -----------------------------------------------------------

    // 接口 12

    /**
     * 获取发现分类<br/>
     *
     * API地址：/mobile/discovery/v1/categories?device=android&picVersion=10&scale=2
     *
     * @return  JSONObject
     */
    public static JSONObject getDiscoverCategories(){
        JSONObject ret = null;

        byte[] data = HttpTools.doGet(API_POINT +
                "/mobile/discovery/v1/categories?device=android&picVersion=10&scale=2");

        if (data != null) {

            try {
                String str = new String(data, "UTF-8");

                ret = new JSONObject(str);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        return ret;
    }

    //---------------------------------------------------------------------------------

    // 接口 11

    /**
     * 获取发现部分，推荐列表的内容
     * 请求网址：/mobile/discovery/v1/recommends
     *                      ?channel=and-f6
     *                      &device=android
     *                      &includeActivity=true
     *                      &includeSpecial=true
     *                      &scale=2
     *                      &version=4.1.7.1
     * @param channel 软件发布渠道
     * @param includeActivity 是否包含活动
     * @param includeSpecial 是否包含 “精品听单”
     * @return
     */
    public static JSONObject getDiscoverRecommend(
            String channel, Boolean includeActivity, Boolean includeSpecial){

        JSONObject ret = null;

        if (channel == null) {

            channel = "and-f6";

        }

        String url = API_POINT +
                "/mobile/discovery/v1/recommends" +
                "?channel=" +channel +
                "&device=android" +
                "&includeActivity=" + includeActivity +
                "&includeSpecial=" + includeSpecial +
                "&scale=2" +
                "&version=4.1.7.1";

        byte[] data = HttpTools.doGet(url);

        if (data != null) {

            try {

                String str = new String(data, "UTF-8");
                ret = new JSONObject(str);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        return ret;
    }


}
